package com.inetools.gwetons.server.logging;

import com.google.gwt.core.client.GWT;
import com.google.gwt.inject.rebind.adapter.GinModuleAdapter;
import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.inetools.gwetons.client.logging.LogUtilGinModule;
import com.inetools.gwetons.client.logging.LogUtilGinjector;
import com.inetools.gwetons.shared.logging.LogUtil;

public class LogUtilInjector implements LogUtilGinjector {
	public static class LogUtilGinModuleAdapter implements Module {
		@Override
		public void configure( Binder binder) {
			binder.install( new GinModuleAdapter( new LogUtilGinModule()));
		}
	}
	private static LogUtilGinModuleAdapter logUtilGinModuleAdapter = new LogUtilGinModuleAdapter();
	
	private static Injector injector;
		public synchronized static Injector getInjector() {
			if ( LogUtilInjector.injector == null) {
				if ( !GWT.isClient()) {
					LogUtilInjector.injector = Guice.createInjector( logUtilGinModuleAdapter);
				}
				else {
					throw new UnsupportedOperationException(
							"By default, it is not supported to use this for GWT client code. " 
							+ "Either set your custom Injector instance to this before actual " 
							+ "injection happens or use JsniLogUtilGinjector or LogUtilGinjector."
							);
				}
			}
			return LogUtilInjector.injector;
		}
		public synchronized static void setInjector( Injector injector) {
			LogUtilInjector.injector = injector;
		}
		
	@Override
	public LogUtil getLogUtil() {
		return LogUtilInjector.getInjector().getInstance( LogUtil.class);
	}
}
